import { Component, OnInit, ViewChild } from '@angular/core';
import { Route } from "@angular/router";
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';
@Component({
  selector: 'product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.scss'],
  animations: [
    flyIn
  ]
})
export class Product implements OnInit {
  @ViewChild('myModal') public myModal: ModalDirective;
  proData: any;
  proname: any;
  prodes: any;
  product_name: any;
  product_description: any;
  productname: any;
  productdescription: any;
  product_id: any;
  proconnect: any = '';
  public connectType = ['Wifi', '4G', '2G', 'NBiot'];

  btype: number;
  constructor(private myHttp: MyHttp) { }
  ngOnInit() {
    this.getdata();
  }

  cancel() {
    this.productname = "";
    this.productdescription = "";
    this.proconnect = "";
  }

  getdata() {
    this.myHttp.post('product/findAll' ,{}).subscribe(res => {
      if (res.errorCode == 0) {
        this.proData = res.message;
      }
    })
  }

  addsave() {
    this.myHttp.post('product/create', {
      "product_name": this.proname,
      "product_description": this.prodes,
      "product_connection": this.proconnect
    }).subscribe(res => {
      if (res.errorCode == 0) {
        alert(res.message);
        this.myModal.hide();
        this.getdata();
        this.prodes = '';
        this.proname = '';
        this.proconnect = '';
      }

    }, err => { alert('创建失败') })
  }

  editproduct(pdata) {
    this.myModal.show();
    this.btype = 2;
    console.log(pdata)
    this.product_id = pdata.id;
    this.productname = pdata.product_name;
    this.productdescription = pdata.product_description;
    this.proconnect = pdata.product_connection;
  }

  editsave() {
    this.myHttp.post('product/update', {
      "product_id": this.product_id,
      "product_name": this.productname,
      "product_description": this.productdescription
    }).subscribe(res => {
      if (res.errorCode == 0) {
        alert(res.message);
        this.myModal.hide();
        this.getdata();
      }

    })
  }

  delproduct(prodata) {
    this.btype = 3;
    this.myModal.show();
    this.product_id = prodata.id;
    this.product_name = prodata.product_name;
  }
 
  delsave() {
    this.myHttp.post('product/delete', {
      "product_id": this.product_id
    }).subscribe(res => {
      if (res.errorCode == 0) {
        alert(res.message);
        this.myModal.hide();
        this.getdata();
      }
    })
  }
}

